﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FulcrumApi.clients;
using FulcrumApi.entities;
using Newtonsoft.Json;

namespace FulcrumApi.services
{
    public class StoryServices
    {
        private FulcrumHttpClient client;

        public StoryServices(FulcrumHttpClient client)
        {
            this.client = client;
        }

        public Story CreateStory(string name, string description, StoryType storyType, StoryState storyState, int estimate, int projectId)
        {
            var story = new StoryCreated.StoryCreatedStory();
            story.state = Enum.GetName(typeof(StoryState), storyState);
            story.story_type = Enum.GetName(typeof(StoryType), storyType);
            story.editing = true;
            story.title = name;
            story.description = description;
            story.estimate = estimate;

            var fulcrumStory = new StoryCreated();
            fulcrumStory.story = story;

            var res = client.doPost(String.Format("/projects/{0}/stories.js", projectId), fulcrumStory);
            var reader = new StreamReader(res);
            var content = reader.ReadToEnd();
            return new Story(JsonConvert.DeserializeObject<FulcrumStory>(content).story);
        }

        public List<Story> ListStories(int projectId)
        {

            Stream json = client.doGet(String.Format("/projects/{0}/stories.json", projectId), new Dictionary<String, String>());
            StreamReader reader = new StreamReader(json);
            var content = reader.ReadToEnd();
            var stories = JsonConvert.DeserializeObject<List<FulcrumStory>>(content);
            var realStories = new List<Story>();
            if (stories != null)
            {
                foreach (var storey in stories)
                {
                    realStories.Add(new Story(storey.story));

                }
            }
            return realStories.OrderBy(s => s.Position).ToList();
        }
        
        public Story ReadStory(int storyId, int projectId)
        {
        	return (from s in ListStories(projectId) where s.Id == storyId select s).First();
        }

        public void DeleteStory(int projectId, Story story)
        {
        	DeleteStory(projectId, story.Id);
        }

        public void DeleteStory(int projectId, int storyId)
        {
            client.doDelete(String.Format("/projects/{0}/stories/{1}.json", projectId, storyId));
        }

        public void MoveStoryAfter(Story storyFeature, Story storyChore)
        {
            var allStories = ListStories(storyChore.ProjectId);
            int i;
            for (i = 0; i < allStories.Count; i++)
            {
                if (allStories[i].Id.Equals(storyChore.Id))
                {
                    if (i + 1 < allStories.Count)
                    {
                        storyFeature.Position = ((allStories[i].Position + allStories[i + 1].Position) / 2);
                    }
                    else
                    {
                        storyFeature.Position = allStories[i].Position + 1;
                    }
                    UpdateStory(storyFeature);
                    break;
                }
            }
        }

        public Story UpdateStory(Story story)
        {

            var fulcrumStory = new FulcrumStory();
            fulcrumStory.story = story.story;

            var res = client.doPut(String.Format("/projects/{0}/stories/{1}.js", story.ProjectId, story.Id), fulcrumStory);
            var reader = new StreamReader(res);
            var content = reader.ReadToEnd();
            return new Story(JsonConvert.DeserializeObject<JsonStory>(content));
        }

    }
}
